package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.annotation.Title;
import com.heima.dto.FieldDto;
import com.heima.dto.InsertDictionaryDto;
import com.heima.dto.pagedto.PageDictionaryDto;
import com.heima.service.DictionaryService;
import com.heima.vo.*;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "字典模块")
@Title("字典模块")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    /**
     * 字典类型分页查询
     * @return
     */
    @GetMapping("/system/dict/type/list")
    @OperationFill
    public PageResult sysNormalDisable(PageDictionaryDto dto,
                                       @RequestParam(value = "params[beginTime]",required = false) String begin,
                                       @RequestParam(value = "params[endTime]",required = false) String end ) throws Exception {
        log.info("字典类型分页查询:{},{},{}",dto,begin,end);
        dto.setBeginTime(begin);
        dto.setEndTime(end);
        PageResult vo = dictionaryService.queryByDictionary(dto);
        return vo;
    }

    /**
     * 字典字段具体内容分页查询
     * @return
     */
    @GetMapping("/system/dict/type/{id}")
    @OperationFill
    public FieldTypeOneVo sysNormalDisable(@PathVariable Integer id){
        log.info("字典字段具体内容分页查询:{}",id);
        FieldTypeOneVo vo = dictionaryService.queryByDictionaryFiled(id);
        return vo;
    }

    /**
     * 新增字典类型
     */
    @PostMapping("/system/dict/type")
    @OperationFill
    public String insertDictionaryType(@RequestBody  InsertDictionaryDto dto){
        log.info("新增字典类型:{}",dto);
        dictionaryService.insertDictionaryType(dto);
        return "OK";
    }

    /**
     * 修改字典类型
     */
    @PutMapping("/system/dict/type")
    @OperationFill
    public String updateDictionaryType(@RequestBody  InsertDictionaryDto dto){
        log.info("修改字典类型:{}",dto);
        dictionaryService.updateDictionaryType(dto);
        return "OK";
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/system/dict/type/{ids}")
    @OperationFill
    public String deleteDictionaryType(@PathVariable List<Integer> ids){
        log.info("删除字典类型");
        dictionaryService.deleteDictionaryType(ids);
        return "OK";
    }

    /**
     * 新增字典字段内容
     */
    @PostMapping("/system/dict/data")
    @OperationFill
    public String insertDictionaryField(@RequestBody  FieldDto dto){
        log.info("新增字典字段内容:{}",dto);
        dictionaryService.insertDictionaryField(dto);
        return "OK";
    }

    /**
     * 根据id查询字典字段内容
     */
    @GetMapping("/system/dict/data/{id}")
    @OperationFill
    public FieldOneVo findFieldById(@PathVariable Integer id){
        log.info("根据id查询字典字段内容");
        FieldOneVo vo = dictionaryService.findFieldById(id);
        return vo;
    }

    /**
     * 根据id修改字典字段内容
     */
    @PutMapping("/system/dict/data")
    @OperationFill
    public String updateField(@RequestBody FieldDto dto){
        log.info("根据id修改:{}",dto);
        dictionaryService.updateField(dto);
        return "OK";
    }

    /**
     * 根据类型查询字典字段
     */
    @GetMapping("/system/dict/data/list")
    @OperationFill
    public PageResult queryDictionaryField(PageDictionaryDto dto){
        log.info("根据id查询字典字段：{}",dto);
        PageResult vo = dictionaryService.queryByDictionaryType(dto);
        return vo;
    }

    /**
     * 根据id删除字典字段内容
     */
    @DeleteMapping("/system/dict/data/{ids}")
    @OperationFill
    public String  deleteDictionaryField(@PathVariable List<Integer> ids){
        log.info("根据id删除字典内容:{}",ids);
        dictionaryService.deleteDictionaryField(ids);
        return "OK";
    }

    /**
     * 字典组件在其他页面的分页查询
     */
    @GetMapping("/system/dict/data/type/{fieldName}")
    public FieldListVo queryField(@PathVariable String fieldName){
        log.info("字典组件在其他页面的分页查询");
        FieldListVo vo = dictionaryService.queryField(fieldName);
        return vo;
    }

    /**
     * 商机池捞取
     */
    @PutMapping("/business/gain")
    public ResultVo scooping(@RequestBody List<Integer> ids){
        log.info("商机池捞取");
        dictionaryService.scooping(ids);
        return new ResultVo();
    }
}
